package com.openwater;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.openwater.network.FileDownloader;
import com.openwater.network.FileDownloader.ProgressListener;
import com.openwater.tools.StringUtil;

public class DownLoadActivity extends Activity {

    // private ListView mListView = null;
    private EditText mUrlText = null;
    private Button mStartBtn = null;
    private TextView fileName;
    private TextView mPercent;
    private MyPrograssBar progressBar;
    private Button pause;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 0:
                Bundle bundle = msg.getData();
                bundle.getFloat("totalSize");
                bundle.getFloat("speed");
                bundle.getFloat("remainTime");
                float percent = bundle.getFloat("size") / bundle.getFloat("totalSize");
                fileName.setText(bundle.getString("fileName"));
                mPercent.setText(StringUtil.formatPercent(percent));
                progressBar.setProgress((int) (percent * 100));

                break;
            case 1:
                mStartBtn.setEnabled(true);
                break;
            default:
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_view);
        mUrlText = (EditText) findViewById(R.id.download_url);
        // for test
        mUrlText.setText("http://123.234.41.23:9000/attachments/download/1066/errlog.txt");
        // for test end
        // mListView = (ListView) findViewById(R.id.list_view);
        // mListView.setAdapter(mAdapter);
        fileName = (TextView) findViewById(R.id.fileName);
        mPercent = (TextView) findViewById(R.id.percent);
        progressBar = (MyPrograssBar) findViewById(R.id.progressBar1);
        pause = (Button) findViewById(R.id.pause);
        mStartBtn = (Button) findViewById(R.id.start_download);
        mStartBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final String url = mUrlText.getText().toString();
                if (url != null && URLUtil.isHttpUrl(url)) {
                    mStartBtn.setEnabled(false);
                    final FileDownloader downloader = new FileDownloader(url);
                    downloader.download(new ProgressListener() {

                        @Override
                        public void onDownloadProcess(int size, int totalSize, float speed,
                                int remainTime) {
                            Message msg = mHandler.obtainMessage(0);
                            Bundle bundle = msg.getData();
                            bundle.putFloat("size", size);
                            bundle.putFloat("totalSize", totalSize);
                            bundle.putFloat("speed", speed);
                            bundle.putFloat("remainTime", remainTime);
                            bundle.putString("fileName", downloader.getFileName());
                            msg.setData(bundle);
                            msg.sendToTarget();
                            if (downloader.isFinish()) {
                                mHandler.sendEmptyMessage(1);
                            }
                        }

                        @Override
                        public void onDownloadFail(String fileName) {
                            mHandler.sendEmptyMessage(1);
                        }
                    });
                } else {
                    Toast.makeText(getApplicationContext(), "URL is not Available!",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // private BaseAdapter mAdapter = new BaseAdapter() {
    //
    // private LayoutInflater mInflater = null;
    //
    // @Override
    // public View getView(final int position, View convertView, ViewGroup
    // parent) {
    // if (convertView == null) {
    // ViewHolder holder = new ViewHolder();
    // mInflater = LayoutInflater.from(getApplicationContext());
    // convertView = mInflater.inflate(R.layout.download_list_item, null);
    // holder.fileName = (TextView) convertView.findViewById(R.id.fileName);
    // holder.percent = (TextView) convertView.findViewById(R.id.percent);
    // holder.progressBar = (MyPrograssBar)
    // convertView.findViewById(R.id.progressBar1);
    // holder.pause = (Button) convertView.findViewById(R.id.pause);
    // holder.pause.setOnClickListener(new OnClickListener() {
    // @Override
    // public void onClick(View v) {
    // FileDownloader downlaodItem = mList.get(position);
    // if (downlaodItem != null) {
    // downlaodItem.setPause(true);
    // }
    // }
    // });
    // convertView.setTag(holder);
    // }
    // ViewHolder holder = (ViewHolder) convertView.getTag();
    // FileDownloader downlaodItem = mList.get(position);
    // if (downlaodItem != null) {
    // holder.fileName.setText(downlaodItem.getFileName());
    // holder.percent.setText(StringUtil.formatPercent(downlaodItem.getPersent()));
    // holder.progressBar.setProgress((int) (downlaodItem.getPersent() * 100));
    // }
    //
    // return convertView;
    // }
    //
    // @Override
    // public long getItemId(int position) {
    // // TODO Auto-generated method stub
    // return 0;
    // }
    //
    // @Override
    // public Object getItem(int position) {
    // // TODO Auto-generated method stub
    // return null;
    // }
    //
    // @Override
    // public int getCount() {
    // return mList.size();
    // }
    // };

    private class ViewHolder {
        private TextView fileName;
        private TextView percent;
        private Button pause;
        private MyPrograssBar progressBar;
    }
}
